<?php

namespace common\helpers;

use common\base\BaseActiveRecord;
use common\models\Ad;
use common\models\Banner;
use common\models\Exhibition;
use common\models\ExhibitionHallPicture;
use common\models\FrontendMenu;
use common\models\User;
use common\models\UserCompany;
use common\models\UserCreditLog;
use common\models\UserFollow;
use common\models\UserHistory;
use common\models\UserReply;
use common\models\Weather;
use Yii;

class TStringHelper
{

    /**
     * 数据状态
     * @param null $status
     * @return array|mixed
     */
    public static function getStatusText($status = null, $view = false){
        $statusArray = array(
            1 => Yii::t('common', 'status_normal'),
            0 => Yii::t('common', 'status_temp'),
            2 => Yii::t('common', 'status_stop'),
        );
        if (is_null($status)){
            return $view ? '' : $statusArray;
        }else{
            return isset($statusArray[$status]) ? $statusArray[$status] : null;
        }
    }

    /**
     * 时间显示 格式
     * @param $date
     * @param int $formate
     * @return bool|string
     */
    public static function ShowDate($date, $formate = 1){
        if (empty($date)) return $date;
        switch ($formate){
            case 1:
                return date('Y-m-d H:i:s', $date);

            case 2:
                return date('Y-m-d H:i', $date);

            case 3:
                return date('Y-m-d', $date);

            default: break;
        }
    }

    /**
     * 获取资讯类型
     * @param null $type
     * @return array|mixed
     */
    public static function getNewsType($type = null, $view = false){
        $typeArray = array(
            0 => '公告',
            //1 => '新闻',
            //2 => '其它',
        );
        if (is_null($type)){
            return $view ? '' : $typeArray;
        }else{
            return $typeArray[$type];
        }
    }

    /**
     * 判断字符串是否为UTF-8
     * @param $str
     * @return bool
     */
    public static function isUTF8($str)
    {
        if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * UTF-8 字符串截取
     * @param $str 待截取的字符串
     * @param $start 截取开始位置
     * @param $num 截取长度
     * @param null $suffix 尾缀
     * @return string
     */
    public static function utf8_substr($str, $start, $num, $suffix = null)
    {
        $res = '';      //存储截取到的字符串
        $cnt = 0;       //计数器，用来判断字符串是否走到$start位置
        $t = 0;         //计数器，用来判断字符串已经截取了$num的数量
        for ($i = 0; $i < strlen($str); $i++) {
            if (ord($str[$i]) > 127) {    //非ascii码时
                if ($cnt >= $start) {     //如果计数器走到了$start的位置
                    $res .= $str[$i] . $str[++$i] . $str[++$i]; //utf-8是三字节编码，$i指针连走三下，把字符存起来
                    $t = $t + 2;              //计数器++，表示我存了几个字符串了到$num的数量就退出了
                } else {
                    $i++;               //如果没走到$start的位置，那就只走$i指针，字符不用处理
                    $i++;
                }
                $cnt++;
            } else {
                if ($cnt >= $start) {     //acsii码正常处理就好
                    $res .= $str[$i];
                    $t++;
                }
                $cnt++;
            }
            if ($num <= $t) {
                $i++;
                if ($str[$i] && $suffix != null && !empty($suffix)) {
                    $res .= $suffix;
                }
                break;       //ok,我要截取的数量已经够了，我不贪婪，我退出了
            }
        }
        return $res;
    }

    /**
     * 编辑器分页
     * @param $content
     * @return string
     */
    public static function pageBreak($content){
        $pattern  = "/<div style=\"page-break-after: always\"><span style=\"display:none\">&nbsp;<\/span><\/div>/i";
        $strSplit = preg_split($pattern, $content, -1, PREG_SPLIT_NO_EMPTY);
        $count    = count($strSplit);
        //$outStr   = "";
        $outStr   = [];
        //$i        = 1;

        if ($count > 1 ) {
            return [
                'pageCount' => $count,
                'data' => $strSplit,
            ];
            /*$outStr   = "<div id='page_break'>";
            foreach($strSplit as $value) {
                if ($i <= 1) {
                    $outStr .= "<div id='page_$i'>$value</div>";
                } else {
                    $outStr .= "<div id='page_$i' class='collapse'>$value</div>";
                }
                $i ++;
            }*/

            /*$outStr .= "<ul class='num'>";
            for ($i = 1; $i <= $count; $i++ ) {
                $outStr .= "<li>$i</li>";
            }
            $outStr .= "</ul></div>";
            return $outStr;*/
        } else {
            return [
                'pageCount' => 1,
                'data' => [
                    0 => $content,
                ],
            ];
        }
    }

    /**
     * 获取用户所在城市
     * @return bool
     */
    public static function getClientCity(){
        $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php';
        $ip = TNetworkHelper::getClientRealIP();
        $res = TNetworkHelper::HttpGet($url, ['format' => 'json', 'ip' => $ip]);
        if (isset($res['code']) && $res['code'] == 200) {
            $content = json_decode($res['content'], true);
            if (!empty($content)) {
                return $content['city'];
            }
        }
        return false;
    }

    /**
     * 获取天气信息
     * @param string $city
     * @return bool|string
     */
    public static function getWeatherInfo($city = ''){
        $city = empty($city) ? self::getClientCity() : $city;
        $model = Weather::find(false)->andWhere("cityNAME like :cityName", [':cityName' => "%$city%"])->orderBy('id asc')->one();
        if (empty($model)) return false;
        $url = 'http://tj.nineton.cn/Heart/index/all';
        //$url = 'http://tj.nineton.cn/Heart/index/future24h/';
        $res = TNetworkHelper::HttpGet($url, ['city' => $model['townID']]);
        if (isset($res['code']) && $res['code'] == 200) {
            $content = json_decode($res['content'], true);
            if ($content['status'] == 'OK') {
                //天气状况 当日最高气温/当日最低气温
                //return $content['now']['text']. ' '.$content['future'][0]['high'].'/'.$content['future'][0]['low'];
                return $content['weather'][0];
            }
        }
        return false;
    }

    /**
     * 计算当前农历日期
     * 调用 nongli('20160807'); //标准8位
     * @param $riqi
     * @return string
     */
    public static function nongli($riqi = '')
    {
        if (empty($riqi)) $riqi = date('Ymd');
        $nian=date('Y',strtotime($riqi));
        $yue=date('m',strtotime($riqi));
        $ri=date('d',strtotime($riqi));

        #源码部分原作者：沈潋(S&S Lab)
        #农历每月的天数
        $everymonth=array(
            0=>array(8,0,0,0,0,0,0,0,0,0,0,0,29,30,7,1),
            1=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,8,2),
            2=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,9,3),
            3=>array(5,29,30,29,30,29,29,30,29,29,30,30,29,30,10,4),
            4=>array(0,30,30,29,30,29,29,30,29,29,30,30,29,0,1,5),
            5=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,2,6),
            6=>array(4,29,30,30,29,30,29,30,29,30,29,30,29,30,3,7),
            7=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,4,8),
            8=>array(0,30,29,29,30,30,29,30,29,30,30,29,30,0,5,9),
            9=>array(2,29,30,29,29,30,29,30,29,30,30,30,29,30,6,10),
            10=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,7,11),
            11=>array(6,30,29,30,29,29,30,29,29,30,30,29,30,30,8,12),
            12=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,9,1),
            13=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,10,2),
            14=>array(5,30,30,29,30,29,30,29,30,29,30,29,29,30,1,3),
            15=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,2,4),
            16=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,3,5),
            17=>array(2,30,29,29,30,29,30,30,29,30,30,29,30,29,4,6),
            18=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,5,7),
            19=>array(7,29,30,29,29,30,29,29,30,30,29,30,30,30,6,8),
            20=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,7,9),
            21=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,8,10),
            22=>array(5,30,29,30,30,29,29,30,29,29,30,29,30,30,9,11),
            23=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,10,12),
            24=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,1,1),
            25=>array(4,30,29,30,29,30,30,29,30,30,29,30,29,30,2,2),
            26=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,3,3),
            27=>array(0,30,29,29,30,29,30,29,30,29,30,30,30,0,4,4),
            28=>array(2,29,30,29,29,30,29,29,30,29,30,30,30,30,5,5),
            29=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,6,6),
            30=>array(6,29,30,30,29,29,30,29,29,30,29,30,30,29,7,7),
            31=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,8,8),
            32=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,9,9),
            33=>array(5,29,30,30,29,30,30,29,30,29,30,29,29,30,10,10),
            34=>array(0,29,30,29,30,30,29,30,29,30,30,29,30,0,1,11),
            35=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,2,12),
            36=>array(3,30,29,29,30,29,29,30,30,29,30,30,30,29,3,1),
            37=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,4,2),
            38=>array(7,30,30,29,29,30,29,29,30,29,30,30,29,30,5,3),
            39=>array(0,30,30,29,29,30,29,29,30,29,30,29,30,0,6,4),
            40=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,7,5),
            41=>array(6,30,30,29,30,30,29,30,29,29,30,29,30,29,8,6),
            42=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,9,7),
            43=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,10,8),
            44=>array(4,30,29,30,29,30,29,30,29,30,30,29,30,30,1,9),
            45=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,2,10),
            46=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,3,11),
            47=>array(2,30,30,29,29,30,29,29,30,29,30,29,30,30,4,12),
            48=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,5,1),
            49=>array(7,30,29,30,30,29,30,29,29,30,29,30,29,30,6,2),
            50=>array(0,29,30,30,29,30,30,29,29,30,29,30,29,0,7,3),
            51=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,8,4),
            52=>array(5,29,30,29,30,29,30,29,30,30,29,30,29,30,9,5),
            53=>array(0,29,30,29,29,30,30,29,30,30,29,30,29,0,10,6),
            54=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,1,7),
            55=>array(3,29,30,29,30,29,29,30,29,30,29,30,30,30,2,8),
            56=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,3,9),
            57=>array(8,30,29,30,29,30,29,29,30,29,30,29,30,29,4,10),
            58=>array(0,30,30,30,29,30,29,29,30,29,30,29,30,0,5,11),
            59=>array(0,29,30,30,29,30,29,30,29,30,29,30,29,0,6,12),
            60=>array(6,30,29,30,29,30,30,29,30,29,30,29,30,29,7,1),
            61=>array(0,30,29,30,29,30,29,30,30,29,30,29,30,0,8,2),
            62=>array(0,29,30,29,29,30,29,30,30,29,30,30,29,0,9,3),
            63=>array(4,30,29,30,29,29,30,29,30,29,30,30,30,29,10,4),
            64=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,1,5),
            65=>array(0,29,30,29,30,29,29,30,29,29,30,30,29,0,2,6),
            66=>array(3,30,30,30,29,30,29,29,30,29,29,30,30,29,3,7),
            67=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,4,8),
            68=>array(7,29,30,29,30,30,29,30,29,30,29,30,29,30,5,9),
            69=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,6,10),
            70=>array(0,30,29,29,30,29,30,30,29,30,30,29,30,0,7,11),
            71=>array(5,29,30,29,29,30,29,30,29,30,30,30,29,30,8,12),
            72=>array(0,29,30,29,29,30,29,30,29,30,30,29,30,0,9,1),
            73=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,10,2),
            74=>array(4,30,30,29,30,29,29,30,29,29,30,30,29,30,1,3),
            75=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,2,4),
            76=>array(8,30,30,29,30,29,30,29,30,29,29,30,29,30,3,5),
            77=>array(0,30,29,30,30,29,30,29,30,29,30,29,29,0,4,6),
            78=>array(0,30,29,30,30,29,30,30,29,30,29,30,29,0,5,7),
            79=>array(6,30,29,29,30,29,30,30,29,30,30,29,30,29,6,8),
            80=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,7,9),
            81=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,8,10),
            82=>array(4,30,29,30,29,29,30,29,29,30,29,30,30,30,9,11),
            83=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,10,12),
            84=>array(10,30,29,30,30,29,29,30,29,29,30,29,30,30,1,1),
            85=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,2,2),
            86=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,3,3),
            87=>array(6,30,29,30,29,30,30,29,30,30,29,30,29,29,4,4),
            88=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,5,5),
            89=>array(0,30,29,29,30,29,29,30,30,29,30,30,30,0,6,6),
            90=>array(5,29,30,29,29,30,29,29,30,29,30,30,30,30,7,7),
            91=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,8,8),
            92=>array(0,29,30,30,29,29,30,29,29,30,29,30,30,0,9,9),
            93=>array(3,29,30,30,29,30,29,30,29,29,30,29,30,29,10,10),
            94=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,1,11),
            95=>array(8,29,30,30,29,30,29,30,30,29,29,30,29,30,2,12),
            96=>array(0,29,30,29,30,30,29,30,29,30,30,29,29,0,3,1),
            97=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,4,2),
            98=>array(5,30,29,29,30,29,29,30,30,29,30,30,29,30,5,3),
            99=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,6,4),
            100=>array(0,30,30,29,29,30,29,29,30,29,30,30,29,0,7,5),
            101=>array(4,30,30,29,30,29,30,29,29,30,29,30,29,30,8,6),
            102=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,9,7),
            103=>array(0,30,30,29,30,30,29,30,29,29,30,29,30,0,10,8),
            104=>array(2,29,30,29,30,30,29,30,29,30,29,30,29,30,1,9),
            105=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,2,10),
            106=>array(7,30,29,30,29,30,29,30,29,30,30,29,30,30,3,11),
            107=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,4,12),
            108=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,5,1),
            109=>array(5,30,30,29,29,30,29,29,30,29,30,29,30,30,6,2),
            110=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,7,3),
            111=>array(0,30,29,30,30,29,30,29,29,30,29,30,29,0,8,4),
            112=>array(4,30,29,30,30,29,30,29,30,29,30,29,30,29,9,5),
            113=>array(0,30,29,30,29,30,30,29,30,29,30,29,30,0,10,6),
            114=>array(9,29,30,29,30,29,30,29,30,30,29,30,29,30,1,7),
            115=>array(0,29,30,29,29,30,29,30,30,30,29,30,29,0,2,8),
            116=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,3,9),
            117=>array(6,29,30,29,30,29,29,30,29,30,29,30,30,30,4,10),
            118=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,5,11),
            119=>array(0,30,29,30,29,30,29,29,30,29,29,30,30,0,6,12),
            120=>array(4,29,30,30,30,29,30,29,29,30,29,30,29,30,7,1)
        );
        ##############################
        #农历天干
        $mten=array("null","甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
        #农历地支
        $mtwelve=array("null","子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)",
            "巳(蛇)","午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)");
        #农历月份
        $mmonth=array("闰","正","二","三","四","五","六",
            "七","八","九","十","十一","十二","月");
        #农历日
        $mday=array("null","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
            "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
            "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十");
        ##############################
        #星期
        $weekday = array("周日","周一","周二","周三","周四","周五","周六");
        #阳历总天数 至1900年12月21日
        $total=11;
        #阴历总天数
        $mtotal=0;
        ##############################
        #获得当日日期
        //$today=getdate(); //获取今天的日期
        if($nian<1901 || $nian>2020) die("年份出错！");
        //$cur_wday=$today["wday"]; //星期中第几天的数字表示
        for($y=1901;$y<$nian;$y++) { //计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和
            $total+=365;
            if ($y%4==0) $total++;
        }
        switch($yue) { //再加当年的几个月
            case 12:
                $total+=30;
            case 11:
                $total+=31;
            case 10:
                $total+=30;
            case 9:
                $total+=31;
            case 8:
                $total+=31;
            case 7:
                $total+=30;
            case 6:
                $total+=31;
            case 5:
                $total+=30;
            case 4:
                $total+=31;
            case 3:
                $total+=28;
            case 2:
                $total+=31;
        }
        if($nian%4 == 0 && $yue>2) $total++; //如果当年是闰年还要加一天
        $total=$total+$ri-1; //加当月的天数
        $flag1=0; //判断跳出循环的条件
        $j=0;
        while ($j<=120){ //用农历的天数累加来判断是否超过阳历的天数
            $i=1;
            while ($i<=13){
                $mtotal+=$everymonth[$j][$i];
                if ($mtotal>=$total){
                    $flag1=1;
                    break;
                }
                $i++;
            }
            if ($flag1==1) break;
            $j++;
        }
        if($everymonth[$j][0]<>0 and $everymonth[$j][0]<$i){ //原来错在这里，对闰月没有修补
            $mm=$i-1;
        }
        else{
            $mm=$i;
        }
        if($i==$everymonth[$j][0]+1 and $everymonth[$j][0]<>0) {
            $nlmon=$mmonth[0].$mmonth[$mm];#闰月
        }
        else {
            $nlmon=$mmonth[$mm].$mmonth[13];
        }
        #计算所求月份1号的农历日期
        $md=$everymonth[$j][$i]-($mtotal-$total);
        if($md > $everymonth[$j][$i])
            $md-=$everymonth[$j][$i];
        $nlday=$mday[$md];

        //$nowday=date("n月j日 ")."w".$weekday[$cur_wday]." ".$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年".$nlmon.$nlday;
        //$nowday=$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年 ".$nlmon.$nlday;
        return $nlmon.$nlday;
    }

    /**
     * 图片截取
     * @param $url
     * @param int $output_width
     * @param int $output_height
     * @param bool $saveDir
     * @param string $suffix
     * @return bool|null|string
     */
    public static function imageCrop( $url, $output_width=60,$output_height=60,$saveDir=false,$suffix='_thumb'){
        if(!file_exists($url)) {
            return false;
        }//判断远程文件@fopen( $url, 'r' )
        try {
			$img_info = @getimagesize($url);   //得到图像的大小
		}catch(\Exception $e) {
			return false;
		}
		if (!is_array($img_info)) return false;
        list($width, $height)=$img_info;

        if( ($width / $height) >= ($output_width / $output_height) )
        {
            $ori_img=array(
                'x' => ceil(($width-$height*$output_width/$output_height)/2) ,
                'y' => 0,
                'w' => ceil($height*$output_width/$output_height) ,
                'h' => $height);
            $op_img=array(
                'x' => 0,
                'y' => 0,
                'w' => $output_width,
                'h' => $output_height);
        }
        else
        {
            $ori_img=array(
                'x' => 0,
                'y' => ceil(($height-$width*$output_height/$output_width)/2),
                'w' => $width,
                'h' => ceil($width*$output_height/$output_width) );
            $op_img=array(
                'x' => 0,
                'y' => 0,
                'w' => $output_width,
                'h' => $output_height);
        }

        switch($img_info[2]){ //取得图片的格式
            case 1:$src=imagecreatefromgif($url);break;
            case 2:$src=imagecreatefromjpeg($url);break;
            case 3:$src=imagecreatefrompng($url);break;
            default:return false;//未知的文件格式
        }
        $dst = imagecreatetruecolor($output_width, $output_height); //新建一个真彩色图像
        imagecopyresampled($dst, $src, $op_img['x'], $op_img['y'], $ori_img['x'], $ori_img['y'], $op_img['w'], $op_img['h'], $ori_img['w'], $ori_img['h']);        //重采样拷贝部分图像并调整大小
        if($saveDir!=false){
            $filename = explode('/' , $url);
            $filename = end($filename);
            $saveDir = $_SERVER['DOCUMENT_ROOT']."/".$saveDir."/".$filename.$suffix.".jpg";
        }else{
            header('Content-Type: image/jpeg');
            $saveDir = null;
        }
        imagejpeg($dst,$saveDir,100);
        imagedestroy($src);
        imagedestroy($dst);
        return $saveDir;
    }

    /**
     * 获取BannerCode
     * @return array
     */
    public static function getBannerCode($code = ''){
        $arr = [
            Banner::BANNER_CODE_INDEX => Yii::t('common', 'home'),
            Banner::BANNER_CODE_NEWS => Yii::t('common', 'news'),
        ];
        if ($code != '') {
            return $arr[$code];
        }
        return $arr;
    }

    /**
     * 获取导航图类型
     * @param string $type
     * @return array|mixed
     */
    public static function getBannerType($type = ''){
        $arr = [
            Banner::BANNER_TYPE_IMAGE => Yii::t('common', 'image'),
            Banner::BANNER_TYPE_VIDEO => Yii::t('common', 'video'),
        ];
        if ($type != '') {
            return $arr[$type];
        }
        return $arr;
    }

    /**
     * 获取判断
     * @param null $val
     * @return array|mixed
     */
    public static function getYesOrNo($val = null){
        $array = array(
            BaseActiveRecord::NO => Yii::t('common', 'no'),
            BaseActiveRecord::YES => Yii::t('common', 'yes'),
        );

        if (is_null($val)){
            return $array;
        }else{
            return $array[$val];
        }
    }

    /**
     * 用户角色
     * @param null $val
     * @return array|mixed
     */
    public static function getUserRole($val = null){
        $array = array(
            User::USER_ROLE_NORMAL => Yii::t('common', 'normal_user'),
            User::USER_ROLE_MODIFYER => Yii::t('common', 'modifyer'),
            User::USER_ROLE_MANAGER => Yii::t('common', 'manager'),
        );

        if (is_null($val)){
            return $array;
        }else{
            return $array[$val];
        }
    }

    /**
     * 推荐等级
     * @param null $val
     * @return array|mixed
     */
    public static function getRecommendLevel($val = null){
        $array = array(
            '1' => 1,
            '2' => 2,
            '3' => 3,
            '4' => 4,
            '5' => 5,
        );

        if (is_null($val)){
            return $array;
        }else{
            return $array[$val];
        }
    }

    /**
     * 判断是否属可以修改数据
     * @param $createBy
     * @return bool
     */
    public static function canBeModified($createBy = null) {
        $loginId = Yii::$app->user->getId();
        if (empty($loginId)) return false;
        $user = User::findOne($loginId);
        //超级管理员
        if ($user->role == 1) {
            return true;
        }
        //部分批量操作按钮
        if (empty($createBy)) return false;
        //指定操作按钮
        if ($loginId == $createBy) {
            return true;
        }
        return false;
    }

    /**
     * 前台菜单位置定义
     * @param null $position
     * @return array|mixed
     */
    public static function getFrontendMenuPosition($position = null){
        $arr = [
            FrontendMenu::POSITION_HEAD => Yii::t('common', 'position_head'),
            FrontendMenu::POSITION_FOOT => Yii::t('common', 'position_foot'),
        ];
        if (empty($position)) {
            return $arr;
        }else{
            return $arr[$position];
        }
    }

    /**
     * 广告类型
     * @param null $type
     * @return array|mixed
     */
    public static function getAdType($type = null) {
        $arr = [
            Ad::AD_TYPE_IMAGE => Yii::t('common', 'image'),
            Ad::AD_TYPE_VIDEO => Yii::t('common', 'video'),
            Ad::AD_TYPE_JS => Yii::t('common', 'js'),
        ];
        if (empty($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 用户余额积分数据类型
     * @param $type
     * @return array|mixed
     */
    public static function getUserCreditType($type = null){
        $arr = [
            UserCreditLog::USER_CREDIT_LOG_TYPE_INTEGRAL => Yii::t('common', 'integral'),
            UserCreditLog::USER_CREDIT_LOG_TYPE_CREDIT => Yii::t('common', 'credit'),
        ];
        if (is_null($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 用户余额积分操作类型
     * @param $type
     * @return array|mixed
     */
    public static function getUserCreditOperType($type = null){
        $arr = [
            UserCreditLog::USER_CREDIT_LOG_OPER_TYPE_BACKEND => Yii::t('common', 'account_change'),
            UserCreditLog::USER_CREDIT_LOG_OPER_TYPE_RECHARGE => Yii::t('common', 'recharge'),
        ];
        if (is_null($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 用户关注类型
     * @param $type
     * @return array|mixed
     */
    public static function getUserFollowType($type = null){
        $arr = [
            UserFollow::USER_FOLLOW_TYPE_EXHIBITION => Yii::t('common', 'exhibition'),
            UserFollow::USER_FOLLOW_TYPE_USER => Yii::t('common', 'user'),
        ];
        if (is_null($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 用户浏览类型
     * @param $type
     * @return array|mixed
     */
    public static function getUserHistoryType($type = null){
        $arr = [
            UserHistory::USER_HISTORY_TYPE_EXHIBITION => Yii::t('common', 'exhibition'),
            UserHistory::USER_HISTORY_TYPE_NEWS => Yii::t('common', 'news'),
        ];
        if (is_null($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 用户回复类型
     * @param $type
     * @return array|mixed
     */
    public static function getUserReplyType($type = null){
        $arr = [
            UserReply::USER_REPLY_TYPE_EXHIBITION => Yii::t('common', 'exhibition'),
            UserReply::USER_REPLY_TYPE_COMMENT => Yii::t('common', 'comment'),
        ];
        if (is_null($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 用户回复类型
     * @param $type
     * @return array|mixed
     */
    public static function getHallPictureType($type = null){
        $arr = [
            ExhibitionHallPicture::HALL_PICTURE_TYPE_PHOTO => Yii::t('common', 'image'),
            ExhibitionHallPicture::HALL_PICTURE_TYPE_VIDEO => Yii::t('common', 'video'),
        ];
        if (is_null($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 展会热门等级
     * @param null $hot
     * @return array|mixed
     */
    public static function getExhibitionHotRank($hot = null) {
        $arr = [
            Exhibition::HOT_ONE => Yii::t('common', 'hot_1'),
            Exhibition::HOT_TWO => Yii::t('common', 'hot_2'),
            Exhibition::HOT_THREE => Yii::t('common', 'hot_3'),
            Exhibition::HOT_FOUR => Yii::t('common', 'hot_4'),
            Exhibition::HOT_FIVE => Yii::t('common', 'hot_5'),
        ];
        if (is_null($hot)) {
            return $arr;
        }else{
            return $arr[$hot];
        }
    }

    /**
     * 用户名检测
     * @param $username
     * @return bool
     */
    public static function check_username($username) {
        $guestexp = '\xA1\xA1|\xAC\xA3|^Guest|^\xD3\xCE\xBF\xCD|\xB9\x43\xAB\xC8';
        $len = self::dstrlen($username);
        if($len > 15 || $len < 3 || preg_match("/\s+|^c:\\con\\con|[%,\*\"\s\<\>\&]|$guestexp/is", $username)) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public static function dstrlen($str) {
        $count = 0;
        for($i = 0; $i < strlen($str); $i++){
            $value = ord($str[$i]);
            if($value > 127) {
                $count++;
                if($value >= 192 && $value <= 223) $i++;
                elseif($value >= 224 && $value <= 239) $i = $i + 2;
                elseif($value >= 240 && $value <= 247) $i = $i + 3;
            }
            $count++;
        }
        return $count;
    }

    /**
     * 检查密码强度
     * @param string $str 密码字符串
     * @return int
     */
    public static function CheckPasswordStrength($str)
    {
        if (strlen($str) < 6) {
            return 0;
        }

        $score = 0;
        if (preg_match("/[0-9]+/", $str)) {
            $score++;
        }

        if (preg_match("/[a-zA-Z]+/", $str)) {
            $score++;
        }
        if (preg_match("/[~|.|,|<|>|;|:|'|\"|_|\-|+|=|*|!|@|#|$|%|^|&|(|)]+/", $str)) {
            $score++;
        }
        return $score;
    }

    /**
     * 返回用户省份与城市信息
     * //province,1,北京|||city,2,通州
     * @param string $userCity
     * @return array|string
     */
    public static function getUserProvince($userCity = '', &$province_id = null, &$city_id = null, &$tmp_name = null){
        if (empty($userCity)) return '';
        $city = explode('|||', $userCity);
        $tmp = array();
        if (!empty($city)) {
            foreach ($city as $val) {
                $area = explode(',', $val);
                if ($area[0] == 'province') $province_id = $area[1];
                if ($area[0] == 'city') $city_id = $area[1];
                $tmp[$area[0]] = array(
                    'id' => $area[1],
                    'name' => $area[2]
                );
                $tmp_name .= ' '.$area[2];
            }
        }

        return $tmp;
    }

    /**
     * 判断是否手机号码格式
     * @param $mobile
     * @return bool
     */
    public static function isMobile($mobile) {
        if (!preg_match("/^1\d{10}$/", $mobile)) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否邮箱格式
     * @param $email
     * @return bool
     */
    public static function isEmail($email) {
//        if (!preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/", $email)) {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            return false;
        }
        return true;
    }

    /**
     * 获取公司类型
     * @param null $type
     * @return array|mixed
     */
    public static function getCompanyType($type = null) {
        $arr = [
            UserCompany::COMPANY_TYPE_Z => Yii::t('common', 'company_zhubanfang'),
            UserCompany::COMPANY_TYPE_C => Yii::t('common', 'company_canzhanshang'),
            UserCompany::COMPANY_TYPE_F => Yii::t('common', 'company_fuwushang'),
        ];
        if (is_null($type)) {
            return $arr;
        }else{
            return $arr[$type];
        }
    }

    /**
     * 公司信息提交状态
     * @param null $status
     * @param bool $view
     * @return array|mixed|null|string
     */
    public static function getCompanyStatusText($status = null, $view = false)
    {
        $statusArray = array(
            1 => Yii::t('common', 'status_success'),
            2 => Yii::t('common', 'status_failure'),
            3 => Yii::t('common', 'status_processed'),
            0 => Yii::t('common', 'status_temp'),
        );
        if (is_null($status)) {
            return $view ? '' : $statusArray;
        } else {
            return isset($statusArray[$status]) ? $statusArray[$status] : null;
        }
    }

    /**
     * 月开始
     * @param $month
     * @return bool|string
     */
    public static function monthStart($month){
        return date('Y-m-d', mktime(0, 0, 0, date($month), 1));
    }

    /**
     * 月结束
     * @param $month
     * @return bool|string
     */
    public static function monthEnd($month){
        return date('Y-m-d', mktime(0, 0, 0,date($month)+1,1)-1);
    }

    /**
     * 颜色
     */
    public static function getColor(){
        return [
            '#ff7f50',
            '#87cefa',
            '#da70d6',
            '#32cd32',
            '#6495ed',
            '#ff69b4',
            '#ba55d3',
            '#cd5c5c',
            '#FFC233',
            '#FF3D67',
            '#059BFF',
        ];
    }

    /**
     * 获取文件缩略图
     * @param $targetFile
     * @param string $width
     * @param string $height
     * @return string
     */
    public static function getImageThumbs($targetFile, $width = '', $height = '') {
        if (empty($width) || empty($height)) return $targetFile;
        if (substr($targetFile, 0, 4) == 'http') return $targetFile;
        $suffix = '_'.$width.'x'.$height;
        $newfile = $targetFile.$suffix.'.jpg';
        $root = Yii::$app->basePath.'/..';
        if (!file_exists($root.$newfile)) {
            $saveDir = substr($targetFile, 0, strripos($targetFile, '/'));
            $saveDir = rtrim($saveDir, '/').'/';
            TStringHelper::imageCrop($root.$targetFile, $width, $height, $saveDir, $suffix);
            return $newfile;
        }else{
            return $newfile;
        }
    }
}